//
//  CarReceptionView.m
//  HondaPro_2
//
//  Created by Eric on 13-6-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "CarReceptionView.h"
#import "MainMenuDataModel.h"
#import "HelpClass.h"
#import "SVProgressHUD.h"
#import "webConfig.h"
#import"webService.h"
@interface CarReceptionView ()

//创建控件
-(void)createStaticLbl;
-(void)initControlsRect;
-(void)createLabelImage;
-(void)createSomeLabels;
-(void)creatTextView;

//刷新控件内容
-(void)freshenViewDis;

@end

@implementation CarReceptionView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        //创建背景
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.frame.size.width, self.frame.size.height-230)];
        lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        [self addSubview:lbl];
        
        [self initControlsRect];
//        [self createLabelImage];
        [self createSomeLabels];
        [self creatTextView];
        ///添加通知
        [self addNotification];
        [self createStaticLbl];
        
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self freshenViewDis];
}
////////////////发送通知
-(void)addNotification
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(searchRepairCar:) name:SEARCHREPAIRCAR object:nil];
}
#pragma mark - 创建控件

-(void)createStaticLbl
{
    //车牌
    [self createAnStaticLblByFrame:[self cgrect:rectForPlateNumberLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"车牌:"];
    
    //车架号
    [self createAnStaticLblByFrame:[self cgrect:rectForVinLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"车架号:"];
    
    //发动机号
    [self createAnStaticLblByFrame:[self cgrect:rectForEngineNumberLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"发动机号:"];
    
    //国家代码
    [self createAnStaticLblByFrame:[self cgrect:rectForCountryCodeLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"国家代码:"];
    
    //生产厂家
    [self createAnStaticLblByFrame:[self cgrect:rectForManufactureNameLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"生产厂家:"];
    
    //变速箱号
    [self createAnStaticLblByFrame:[self cgrect:rectForGearBoxNumberLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"变速箱号:"];
    
    //车种名
    [self createAnStaticLblByFrame:[self cgrect:rectForCarClassNameLbl withLayOut:CGRectMake(-78-10, 0, -58+10, 0)] withTxt:@"车种名:"];
    
    //车型
    [self createAnStaticLblByFrame:[self cgrect:rectForCarTypeLbl withLayOut:CGRectMake(-78-10, 0, -58+10, 0)] withTxt:@"车型:"];
    
    //年款
    [self createAnStaticLblByFrame:[self cgrect:rectForYearLbl withLayOut:CGRectMake(-78-10, 0, -58+10, 0)] withTxt:@"年款:"];
    
    //内饰颜色代码
    [self createAnStaticLblByFrame:[self cgrect:rectForInsideColorCodeLbl withLayOut:CGRectMake(-78-10, 0, -58+10, 0)] withTxt:@"内饰颜色代码:"];
    
    //车体颜色代码
    [self createAnStaticLblByFrame:[self cgrect:rectForCarBodyColorCodeLbl withLayOut:CGRectMake(-78-10, 0, -58+10, 0)] withTxt:@"车体颜色代码:"];
    
    //排气量
    [self createAnStaticLblByFrame:[self cgrect:rectForEmissionLbl withLayOut:CGRectMake(-78-10, 0, -58+10, 0)] withTxt:@"排气量:"];
    
    //钥匙号
    [self createAnStaticLblByFrame:[self cgrect:rectForKeyNumberLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"钥匙号:"];
    
    //上牌日期
    [self createAnStaticLblByFrame:[self cgrect:rectForPlateDataLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"钥匙号:"];
    
    //接待日期
    [self createAnStaticLblByFrame:[self cgrect:rectForCreateDataLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"接待日期:"];
    
    //接待人
    [self createAnStaticLblByFrame:[self cgrect:rectForReceptionPersonNameLbl withLayOut:CGRectMake(-78, 0, -58, 0)] withTxt:@"接待人:"];
    
    //里程数
    [self createAnStaticLblByFrame:[self cgrect:rectForMilemeterLbl withLayOut:CGRectMake(-78, 0, 8-30, 0)] withTxt:@"里程数:"];
    
    //公里
    [self createAnStaticLblByFrame:[self cgrect:rectForMilemeterLbl withLayOut:CGRectMake(100, 0, -40-30, 0)] withTxt:@"KM"];
    
    //重要提示
    [self createAnStaticLblByFrame:[self cgrect:rectForKeyNumberLbl withLayOut:CGRectMake(120+30, -8, -40-30, 0)] withTxt:@"重要提示:"];
    
    self.baoCunBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baoCunBtn setFrame:CGRectMake(837, 258, 95, 38)];
    [self.baoCunBtn setTitle:@"保存" forState:UIControlStateNormal];
    [self.baoCunBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.baoCunBtn setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ht05-1.png"]]];
    self.baoCunBtn.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    [self.baoCunBtn addTarget:self action:@selector(baoCunBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.baoCunBtn];

}
-(void)baoCunBtn:(id)sender
{
    NSLog(@"%@",importantNoticeTxt.text);
//    [SVProgressHUD showWithStatus:@"请等待...." maskType:SVProgressHUDMaskTypeGradient];
//    webService *web = [[webService alloc]init];
//    web.methodName = SEARCHREPAIRCAR;
//        self.carQuery = [[NSMutableArray alloc]init];
//           [self.carQuery addObject:@"whetherHonda"];
//        [web commonGetSharedRequest:SEARCHREPAIRCAR wsparas:self.carQuery];
}
//保存回调
-(void)searchRepairCar:(NSNotification *)notification
{
    NSMutableDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData *)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    NSLog(@"%@",theJson);
    //self.tableViewDataArrX = [NSMutableArray arrayWithArray:[theJson objectForKey:@"customerList"]];
    //[self.detailTableView2 reloadData];
    [SVProgressHUD dismiss];
}
-(void)initControlsRect
{
    rectForPlateNumberLbl =
    CGRectMake(80, 25, 136, 29);
    rectForVinLbl =
    CGRectMake(80, 59, 136, 29);
    rectForEngineNumberLbl =
    CGRectMake(80, 91, 136, 29);
    rectForCarClassNameLbl =
    CGRectMake(318, 25, 136, 29);
    rectForCarTypeLbl =
    CGRectMake(318, 59, 136, 29);
    rectForYearLbl =
    CGRectMake(318, 91, 136, 29);
    rectForCountryCodeLbl =
    CGRectMake(80, 151, 136, 29);
    rectForManufactureNameLbl =
    CGRectMake(80, 183, 136, 29);
    rectForGearBoxNumberLbl =
    CGRectMake(80, 215, 136, 29);
    rectForInsideColorCodeLbl =
    CGRectMake(318, 151, 136, 29);
    rectForCarBodyColorCodeLbl =
    CGRectMake(318, 183, 136, 29);
    rectForEmissionLbl =
    CGRectMake(318, 215, 136, 29);
    rectForKeyNumberLbl =
    CGRectMake(558, 25, 136, 29);
    rectForMilemeterLbl =
    CGRectMake(558, 59, 100, 29);
    rectForPlateDataLbl =
    CGRectMake(558, 91, 136, 29);
    rectForReceptionPersonNameLbl =
    CGRectMake(558, 153, 136, 29);
    rectForCreateDataLbl =
    CGRectMake(558, 208, 136, 29);
    rectForImportantNoticeTxt =
    CGRectMake(718, 44, 209, 200);
    
}

-(void)createLabelImage
{
    UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CarReceptionLblImg.png"]];
    imgv.frame = CGRectMake(0, 10, 938, 250);
    [self addSubview:imgv];
}

-(void)createSomeLabels
{
    plateNumberLbl = [[UILabel alloc] initWithFrame:rectForPlateNumberLbl];
    plateNumberLbl.backgroundColor = [UIColor whiteColor];
    plateNumberLbl.font = [UIFont systemFontOfSize:13];
    plateNumberLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:plateNumberLbl];
    
    vinLbl = [[UILabel alloc] initWithFrame:rectForVinLbl];
    vinLbl.backgroundColor = [UIColor whiteColor];
    vinLbl.font = [UIFont systemFontOfSize:13];
    vinLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:vinLbl];

    engineNumberLbl = [[UILabel alloc] initWithFrame:rectForEngineNumberLbl];
    engineNumberLbl.backgroundColor = [UIColor whiteColor];
    engineNumberLbl.font = [UIFont systemFontOfSize:13];
    engineNumberLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:engineNumberLbl];
    
    carClassNameLbl = [[UILabel alloc] initWithFrame:rectForCarClassNameLbl];
    carClassNameLbl.backgroundColor = [UIColor whiteColor];
    carClassNameLbl.font = [UIFont systemFontOfSize:13];
    carClassNameLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:carClassNameLbl];

    carTypeLbl = [[UILabel alloc] initWithFrame:rectForCarTypeLbl];
    carTypeLbl.backgroundColor = [UIColor whiteColor];
    carTypeLbl.font = [UIFont systemFontOfSize:13];
    carTypeLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:carTypeLbl];
    
    yearLbl = [[UILabel alloc] initWithFrame:rectForYearLbl];
    yearLbl.backgroundColor = [UIColor whiteColor];
    yearLbl.font = [UIFont systemFontOfSize:13];
    yearLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:yearLbl];
    
    countryCodeLbl = [[UILabel alloc] initWithFrame:rectForCountryCodeLbl];
    countryCodeLbl.backgroundColor = [UIColor whiteColor];
    countryCodeLbl.font = [UIFont systemFontOfSize:13];
    countryCodeLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:countryCodeLbl];
    
    manufactureNameLbl = [[UILabel alloc] initWithFrame:rectForManufactureNameLbl];
    manufactureNameLbl.backgroundColor = [UIColor whiteColor];
    manufactureNameLbl.font = [UIFont systemFontOfSize:13];
    manufactureNameLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:manufactureNameLbl];
    
    gearBoxNumberLbl = [[UILabel alloc] initWithFrame:rectForGearBoxNumberLbl];
    gearBoxNumberLbl.backgroundColor = [UIColor whiteColor];
    gearBoxNumberLbl.font = [UIFont systemFontOfSize:13];
    gearBoxNumberLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:gearBoxNumberLbl];
    
    insideColorCodeLbl = [[UILabel alloc] initWithFrame:rectForInsideColorCodeLbl];
    insideColorCodeLbl.backgroundColor = [UIColor whiteColor];
    insideColorCodeLbl.font = [UIFont systemFontOfSize:13];
    insideColorCodeLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:insideColorCodeLbl];
    
    carBodyColorCodeLbl = [[UILabel alloc] initWithFrame:rectForCarBodyColorCodeLbl];
    carBodyColorCodeLbl.backgroundColor = [UIColor whiteColor];
    carBodyColorCodeLbl.font = [UIFont systemFontOfSize:13];
    carBodyColorCodeLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:carBodyColorCodeLbl];
    
    emissionLbl = [[UILabel alloc] initWithFrame:rectForEmissionLbl];
    emissionLbl.backgroundColor = [UIColor whiteColor];
    emissionLbl.font = [UIFont systemFontOfSize:13];
    emissionLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:emissionLbl];
    
    keyNumberLbl =  [[UILabel alloc] initWithFrame:rectForKeyNumberLbl];
    keyNumberLbl.backgroundColor = [UIColor whiteColor];
    keyNumberLbl.font = [UIFont systemFontOfSize:13];
    keyNumberLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:keyNumberLbl];
    
    milemeterLbl = [[UILabel alloc] initWithFrame:rectForMilemeterLbl];
    milemeterLbl.backgroundColor = [UIColor whiteColor];
    milemeterLbl.font = [UIFont systemFontOfSize:13];
    milemeterLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:milemeterLbl];
    
    plateDateLbl = [[UILabel alloc] initWithFrame:rectForPlateDataLbl];
    plateDateLbl.backgroundColor = [UIColor whiteColor];
    plateDateLbl.font = [UIFont systemFontOfSize:13];
    plateDateLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:plateDateLbl];
    
    receptionPersonNameLbl = [[UILabel alloc] initWithFrame:rectForReceptionPersonNameLbl];
    receptionPersonNameLbl.backgroundColor = [UIColor whiteColor];
    receptionPersonNameLbl.font = [UIFont systemFontOfSize:13];
    receptionPersonNameLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:receptionPersonNameLbl];
    
    creatDateLbl = [[UILabel alloc] initWithFrame:rectForCreateDataLbl];
    creatDateLbl.backgroundColor = [UIColor whiteColor];
    creatDateLbl.font = [UIFont systemFontOfSize:13];
    creatDateLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:creatDateLbl];
}

-(void)creatTextView
{
    importantNoticeTxt = [[UITextView alloc] initWithFrame:rectForImportantNoticeTxt];
    importantNoticeTxt.backgroundColor = [UIColor whiteColor];
    //编辑
    importantNoticeTxt.editable = YES;
    importantNoticeTxt.font = [UIFont systemFontOfSize:13];
    importantNoticeTxt.textColor = [UIColor darkGrayColor];
    
    [self addSubview:importantNoticeTxt];
}

-(void)freshenViewDis
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSDictionary *dic = modelIns.carReception;
    plateNumberLbl.text = [dic objectForKey:@"plateNumber"];
    vinLbl.text = [dic objectForKey:@"vin"];
    engineNumberLbl.text = [dic objectForKey:@"engineNumber"];
    carClassNameLbl.text = [dic objectForKey:@"carClassName"];
    carTypeLbl.text = [dic objectForKey:@"carType"];
    yearLbl.text = [dic objectForKey:@"year"];
    countryCodeLbl.text =[dic objectForKey:@"countryCode"];
    manufactureNameLbl.text = [dic objectForKey:@"manufactureName"];
    gearBoxNumberLbl.text = [dic objectForKey:@"gearBoxNumber"];
    insideColorCodeLbl.text = [dic objectForKey:@"insideColorCode"];
    carBodyColorCodeLbl.text = [dic objectForKey:@"carBodyColorCode"];
    emissionLbl.text = [dic objectForKey:@"emission"];
    keyNumberLbl.text = [dic objectForKey:@"keyNumber"];
    milemeterLbl.text = [dic objectForKey:@"milemeter"];
    plateDateLbl.text = [dic objectForKey:@"plateDate"];
    receptionPersonNameLbl.text = [dic objectForKey:@"receptionPersonName"];
    creatDateLbl.text = [dic objectForKey:@"creatDate"];
    
    importantNoticeTxt.text = [dic objectForKey:@"importantNotice"];
}

//给一个CGrect加上一个偏移量
-(CGRect)cgrect:(CGRect)xingCanRect withLayOut:(CGRect)xingCanRectLayOut
{
    return CGRectMake(xingCanRect.origin.x+xingCanRectLayOut.origin.x, xingCanRect.origin.y+xingCanRectLayOut.origin.y, xingCanRect.size.width+xingCanRectLayOut.size.width, xingCanRect.size.height+xingCanRectLayOut.size.height);
}

//创建一个静态lbl
-(void)createAnStaticLblByFrame:(CGRect)xingCanRect withTxt:(NSString *)strTxt
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:xingCanRect];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.font = [UIFont systemFontOfSize:13];
    lbl.textColor = [UIColor darkGrayColor];
    lbl.text = strTxt;
    lbl.textAlignment = UITextAlignmentRight;
    [self addSubview:lbl];
}

@end
